package com.edu.web.servlet;

import com.alibaba.fastjson2.JSON;
import com.edu.entity.Rentals;
import com.edu.entity.User;
import com.edu.service.RentalsService;
import com.edu.service.UserService;
import com.edu.service.imlp.RentalsServiceImpl;
import com.edu.service.imlp.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@WebServlet("/validateUserServlet")
public class ValidateUserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取username
        String username = req.getParameter("username");
        //查询
        User user = userService.queryByusernameUser(username);
        Boolean faly = false;
        //用户名存在
        if (user != null) {
            faly = true;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("state",faly);
        //响应
        String jsonString = JSON.toJSONString(map);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonString);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
